package cn.lgs.com.mobileplayer.view;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.IdRes;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.google.common.collect.Lists;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.lgs.com.mobileplayer.R;
import cn.lgs.com.mobileplayer.base.BaseActivity;
import cn.lgs.com.mobileplayer.base.BaseFragment;

/**
 * Created by lgs on 2017/11/22.
 * 主界面Activity
 * 该项目有很多demo
 */

public class MainActivity extends BaseActivity {

    @BindView(R.id.rg_tab)
    RadioGroup mGroupBtn;   //RadioGroup切换

    @BindView(R.id.rb_location_video)
    RadioButton mLvBtn;

    private List<BaseFragment> fragments;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    private void initFragment() {
        fragments = Lists.newArrayList();
        fragments.add(BaseFragment.newInstance(LocationVideoFragment.class, null));
        fragments.add(BaseFragment.newInstance(LocationAudioFragment.class, null));
        fragments.add(BaseFragment.newInstance(NetworkVideoFragment.class, null));
        fragments.add(BaseFragment.newInstance(NetworkAudioFragment.class, null));
    }

    @Override
    protected void init() {
        initFragment();
        mGroupBtn.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                int id = group.getCheckedRadioButtonId();
                switch (id) {
                    case R.id.rb_location_video:
                        isGrantExternalRW(MainActivity.this);
                        changeFragment(mCurrentFragment, fragments.get(0), R.id.fl_mainContainer);
                        break;
                    case R.id.rb_location_audio:
                        changeFragment(mCurrentFragment, fragments.get(1), R.id.fl_mainContainer);
                        break;
                    case R.id.rb_network_video:
                        changeFragment(mCurrentFragment, fragments.get(2), R.id.fl_mainContainer);
                        break;
                    case R.id.rb_network_audio:
                        changeFragment(mCurrentFragment, fragments.get(3), R.id.fl_mainContainer);
                        break;
                }
            }
        });
        mLvBtn.setChecked(true);
    }


    @OnClick(R.id.rb_function)
    public void click() {
        showToast("功能");
    }

    /**
     * 解决6.0以上版本不能读取外部存储权限的问题
     *
     * @param activity
     * @return
     */
    private static boolean isGrantExternalRW(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                && activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            activity.requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            }, 1);

            return false;
        }

        return true;
    }
}
